﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using jnagr.DBUtility;//Please add references
namespace jnagr.DAL
{
	/// <summary>
	/// 数据访问类:ForestRat
	/// </summary>
	public partial class ForestRat
	{
		public ForestRat()
		{}
		#region  BasicMethod
        public DataSet GetListc(string abh, string harm, string stime, string etime,string mlevel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select hname,CAST(avg(cast(damageCount as decimal(18,2))) as decimal(18,2)) as rates from Vforestrat where 1=1");
            if (harm != "")
                strSql.AppendFormat(" and hname='{0}'", harm);
            if (stime != "")
                strSql.AppendFormat(" and inputTime>='{0}'", stime);
            if (etime != "")
                strSql.AppendFormat(" and inputTime<='{0}'", etime);
            if (mlevel != "" && mlevel != "4,3,2,1")
                strSql.AppendFormat(" and mlevel in ({0})", mlevel);
            if (abh.Length == 6)
            {
                if (abh.EndsWith("000000"))
                    strSql.Append(" group by hname");
                else if (abh.EndsWith("0000"))
                    strSql.Append("and left(abh,2)='" + abh.Substring(0, 2) + "' group by hname");
                else if (abh.EndsWith("00"))
                    strSql.Append("and left(abh,4)='" + abh.Substring(0, 4) + "'group by hname");
                else
                    strSql.Append("and abh='" + abh + "'group by hname");
            }
            else
            {
                strSql.Append("and abh='" + abh + "'group by hname");
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("fr_id", "ForestRat"); 
		}
        public DataSet GetListdis(string devid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT distinct mhid, hname FROM  VForestRat WHERE toolsNums='" + devid + "'");
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int fr_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from ForestRat");
			strSql.Append(" where fr_id=@fr_id");
			SqlParameter[] parameters = {
					new SqlParameter("@fr_id", SqlDbType.Int,4)
			};
			parameters[0].Value = fr_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(jnagr.Model.ForestRat model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into ForestRat(");
			strSql.Append("address,inputTime,plantsName,plantsID,ratName,ratID,investigationMethod,standardnumber,totalUnit,actualCount,damageCount,uid,happenpro)");
			strSql.Append(" values (");
			strSql.Append("@address,@inputTime,@plantsName,@plantsID,@ratName,@ratID,@investigationMethod,@standardnumber,@totalUnit,@actualCount,@damageCount,@uid,@happenpro)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@address", SqlDbType.VarChar,50),
					new SqlParameter("@inputTime", SqlDbType.DateTime),
					new SqlParameter("@plantsName", SqlDbType.VarChar,50),
					new SqlParameter("@plantsID", SqlDbType.Int,4),
					new SqlParameter("@ratName", SqlDbType.VarChar,50),
					new SqlParameter("@ratID", SqlDbType.Int,4),
					new SqlParameter("@investigationMethod", SqlDbType.VarChar,50),
					new SqlParameter("@standardnumber", SqlDbType.VarChar,50),
					new SqlParameter("@totalUnit", SqlDbType.VarChar,50),
					new SqlParameter("@actualCount", SqlDbType.VarChar,50),
					new SqlParameter("@damageCount", SqlDbType.VarChar,50),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@happenpro", SqlDbType.VarChar,50)};
			parameters[0].Value = model.address;
			parameters[1].Value = model.inputTime;
			parameters[2].Value = model.plantsName;
			parameters[3].Value = model.plantsID;
			parameters[4].Value = model.ratName;
			parameters[5].Value = model.ratID;
			parameters[6].Value = model.investigationMethod;
			parameters[7].Value = model.standardnumber;
			parameters[8].Value = model.totalUnit;
			parameters[9].Value = model.actualCount;
			parameters[10].Value = model.damageCount;
			parameters[11].Value = model.uid;
			parameters[12].Value = model.happenpro;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(jnagr.Model.ForestRat model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ForestRat set ");
			strSql.Append("address=@address,");
			strSql.Append("inputTime=@inputTime,");
			strSql.Append("plantsName=@plantsName,");
			strSql.Append("plantsID=@plantsID,");
			strSql.Append("ratName=@ratName,");
			strSql.Append("ratID=@ratID,");
			strSql.Append("investigationMethod=@investigationMethod,");
			strSql.Append("standardnumber=@standardnumber,");
			strSql.Append("totalUnit=@totalUnit,");
			strSql.Append("actualCount=@actualCount,");
			strSql.Append("damageCount=@damageCount,");
			strSql.Append("uid=@uid,");
			strSql.Append("happenpro=@happenpro");
			strSql.Append(" where fr_id=@fr_id");
			SqlParameter[] parameters = {
					new SqlParameter("@address", SqlDbType.VarChar,50),
					new SqlParameter("@inputTime", SqlDbType.DateTime),
					new SqlParameter("@plantsName", SqlDbType.VarChar,50),
					new SqlParameter("@plantsID", SqlDbType.Int,4),
					new SqlParameter("@ratName", SqlDbType.VarChar,50),
					new SqlParameter("@ratID", SqlDbType.Int,4),
					new SqlParameter("@investigationMethod", SqlDbType.VarChar,50),
					new SqlParameter("@standardnumber", SqlDbType.VarChar,50),
					new SqlParameter("@totalUnit", SqlDbType.VarChar,50),
					new SqlParameter("@actualCount", SqlDbType.VarChar,50),
					new SqlParameter("@damageCount", SqlDbType.VarChar,50),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@happenpro", SqlDbType.VarChar,50),
					new SqlParameter("@fr_id", SqlDbType.Int,4)};
			parameters[0].Value = model.address;
			parameters[1].Value = model.inputTime;
			parameters[2].Value = model.plantsName;
			parameters[3].Value = model.plantsID;
			parameters[4].Value = model.ratName;
			parameters[5].Value = model.ratID;
			parameters[6].Value = model.investigationMethod;
			parameters[7].Value = model.standardnumber;
			parameters[8].Value = model.totalUnit;
			parameters[9].Value = model.actualCount;
			parameters[10].Value = model.damageCount;
			parameters[11].Value = model.uid;
			parameters[12].Value = model.happenpro;
			parameters[13].Value = model.fr_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int fr_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ForestRat ");
			strSql.Append(" where fr_id=@fr_id");
			SqlParameter[] parameters = {
					new SqlParameter("@fr_id", SqlDbType.Int,4)
			};
			parameters[0].Value = fr_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string fr_idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ForestRat ");
			strSql.Append(" where fr_id in ("+fr_idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnagr.Model.ForestRat GetModel(int fr_id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 fr_id,address,inputTime,plantsName,plantsID,ratName,ratID,investigationMethod,standardnumber,totalUnit,actualCount,damageCount,uid,happenpro,totalCount from ForestRat ");
			strSql.Append(" where fr_id=@fr_id");
			SqlParameter[] parameters = {
					new SqlParameter("@fr_id", SqlDbType.Int,4)
			};
			parameters[0].Value = fr_id;

			jnagr.Model.ForestRat model=new jnagr.Model.ForestRat();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnagr.Model.ForestRat DataRowToModel(DataRow row)
		{
			jnagr.Model.ForestRat model=new jnagr.Model.ForestRat();
			if (row != null)
			{
				if(row["fr_id"]!=null && row["fr_id"].ToString()!="")
				{
					model.fr_id=int.Parse(row["fr_id"].ToString());
				}
				if(row["address"]!=null)
				{
					model.address=row["address"].ToString();
				}
				if(row["inputTime"]!=null && row["inputTime"].ToString()!="")
				{
					model.inputTime=DateTime.Parse(row["inputTime"].ToString());
				}
				if(row["plantsName"]!=null)
				{
					model.plantsName=row["plantsName"].ToString();
				}
				if(row["plantsID"]!=null && row["plantsID"].ToString()!="")
				{
					model.plantsID=int.Parse(row["plantsID"].ToString());
				}
				if(row["ratName"]!=null)
				{
					model.ratName=row["ratName"].ToString();
				}
				if(row["ratID"]!=null && row["ratID"].ToString()!="")
				{
					model.ratID=int.Parse(row["ratID"].ToString());
				}
				if(row["investigationMethod"]!=null)
				{
					model.investigationMethod=row["investigationMethod"].ToString();
				}
				if(row["standardnumber"]!=null)
				{
					model.standardnumber=row["standardnumber"].ToString();
				}
				if(row["totalUnit"]!=null)
				{
					model.totalUnit=row["totalUnit"].ToString();
				}
				if(row["actualCount"]!=null)
				{
					model.actualCount=row["actualCount"].ToString();
				}
				if(row["damageCount"]!=null)
				{
					model.damageCount=row["damageCount"].ToString();
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["happenpro"]!=null)
				{
					model.happenpro=row["happenpro"].ToString();
				}
                if (row["totalCount"] != null)
                {
                    model.totalCount = row["totalCount"].ToString();
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select fr_id,address,inputTime,plantsName,plantsID,ratName,ratID,investigationMethod,standardnumber,totalUnit,actualCount,damageCount,uid,happenpro ");
			strSql.Append(" FROM ForestRat ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" fr_id,address,inputTime,plantsName,plantsID,ratName,ratID,investigationMethod,standardnumber,totalUnit,actualCount,damageCount,uid,happenpro ");
			strSql.Append(" FROM ForestRat ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM VForestRat ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.fr_id desc");
			}
			strSql.Append(")AS Row, T.*  from VForestRat T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "ForestRat";
			parameters[1].Value = "fr_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

